import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.api.java.JavaSparkContext

class WordCount {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("WordCount")
    val sc = new SparkContext(conf)

    val lines = sc.textFile("hdfs://spark1:9000/spark.txt")
    val words = lines.flatMap(line=>line.split(" "))
    val pairs = words.map(word=>(word, 1))
    val wordCounts = pairs.reduceByKey(_+_)

    wordCounts.foreach(wordCount=> println(wordCount._1 + " appears "+ wordCount._2 +" times."))



  }
}
